package com.tansun.easycare.rule.rulegroup.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.common.Constant;
import com.tansun.easycare.rule.common.DataCache;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.datamodel.service.DataModelService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulegroup.dao.RuleEntryDao;
import com.tansun.easycare.rule.rulegroup.entity.RuleEntry;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroupRef;
import com.tansun.easycare.rule.rulegroup.model.OldRuleGroupRefNew;
import com.tansun.easycare.rule.rulegroup.service.RuleEntryService;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupRefService;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.serialno.service.RuleSerialNoService;
import com.tansun.rule.utils.StringUtils;

@Service
public class RuleEntryServiceImpl extends RuleBaseServiceImpl<RuleEntryDao, RuleEntry> implements RuleEntryService {

	public static final String ENTRY_CODE="ETY";
    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private RuleGroupRefService ruleGroupRefService;

    @Autowired
    private DataModelService dataModelService;

    @Autowired
    private RuleSerialNoService ruleSerialNoService;
    
    @Autowired
    private RuleGroupService ruleGroupService;
    //保存条目
	@Override
	public void saveOrUpdate(RuleEntry ruleEntry) throws BizException {
		String manageId = ruleEntry.getManageId();
    	RuleManage ruleManage = this.ruleManageService.get(manageId);
		if(StringUtils.isNotEmpty(ruleEntry.getId())) {
			RuleEntry re = this.get(ruleEntry.getId());
			dataModelService.saveOrUpdateEntryBuitineParam(ruleManage.getCode(),
														re.getEntryCode(),
														ruleManage.getName(),
														ruleEntry.getEntryName(),
														ruleManage.getProjectCode());
			this.dao.update(ruleEntry);
		}else {
	    	ruleEntry.setEntryCode(ruleSerialNoService.generalCodeByType(ENTRY_CODE, ruleEntry.getProjectCode()));
	    	dataModelService.saveOrUpdateEntryBuitineParam(ruleManage.getCode(),ruleEntry.getEntryCode(),ruleManage.getName(),ruleEntry.getEntryName(),ruleManage.getProjectCode());
	    	Integer salience = ruleManage.getCountSalience() == null ? 999999 :ruleManage.getCountSalience()-1;
	    	ruleEntry.setSort(salience);
	    	ruleManage.setCountSalience(salience);
			ruleManageService.save(ruleManage);
			this.save(ruleEntry);
		}

	}

	@Override
	public void saveRuleEntryRef(List<RuleGroup> groupinfo) throws BizException {
		for(RuleGroup rg:groupinfo) {
			String refid = rg.getRefId();
			if(StringUtils.isNotEmpty(refid)) {
				RuleGroupRef rgf = ruleGroupRefService.get(rg.getRefId());
				rgf.setId(rg.getRefId());
				rgf.setRuleId(rg.getManageId());
				rgf.setPriority(rg.getPriority());
				rgf.setRuleweight(rg.getRuleweight());
				ruleGroupRefService.saveOrUpdate(rgf);
			}
		}
	}
    
	@Override
    public void shiftSort(RuleManage ruleManage, String entryId1, String entryId2, String shiftType) throws BizException {
    	RuleEntry re1 = this.get(entryId1);
    	RuleEntry re2 = this.get(entryId2);
        Integer sal = re1 == null ? 0 : re1.getSort();
        re1.setSort(re2.getSort());
        re2.setSort(sal);
        this.save(re1);
        this.save(re2);
    }

	@Override
	public List<RuleEntry> findListByIds(List<String> ids) {
		if(ids!=null&&ids.size()>0) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("ids", ids);
			return this.dao.findListByIds(map);
		}else {
			return new ArrayList<RuleEntry>();
		}
	}
    
	@Override
	public void deleteByMutilVersion(RuleManage ruleManage, List<String> versions){
		if(versions!=null&&versions.size()>0) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("projectCode", ruleManage.getProjectCode());
			map.put("manageId", ruleManage.getId());
			map.put("versions", versions);
			this.dao.deleteByMutilVersion(map);
	        DataCache.ruleRefEntryGroupChange(ruleManage.getProjectCode());
        }
	}
}
